package com.qdu.study_app.service;

import android.app.Application;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.qdu.study_app.Receiver.TimeCountReceiver;
import com.qdu.study_app.utils.MyCountDownTimer;

public class ClockService extends Service implements MyCountDownTimer.OnTickListener {
    private Application mApplication;
    //private TimeCountReceiver timeCountReceiver;
    private Intent clockIntent;
    //private IntentFilter clockIntentFilter;
    private IntentFilter intentFilter;
    private TimeCountReceiver timeCountReceiver;
    private CountDownTimer countDownTimer;

    public static  int TASK_FINISHED_FLAG = 0;

    public static final String ACTION_TIME_TICK = "com.qdu.study_app.ACTION_TICK";
    public static final String TEXT_TIME = "TIME_CLOCK";
    public static final String ACTION_TASK_TOBACK = "com.qdu.study_app.ACTION_TASK_BACK";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void onCreate() {

        super.onCreate();
     //   onCountDownTick(Long.parseLong(intent.getStringExtra("time"))*60*1000);

    }

    /*
     * 在每次Service启动时调用
     * 有些逻辑希望在每次服务启动时执行就写在这个方法中
     * */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        clockIntent = intent;
        ClockService.TASK_FINISHED_FLAG = 0;
        onCountDownTick(Long.parseLong(intent.getStringExtra("time"))*60*1000);
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("service:销毁————————————————————");
        countDownTimer.cancel();
    }

    @Override
    public void onCountDownTick(long millisUntilFinished) {
        countDownTimer = new CountDownTimer(millisUntilFinished, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                formatTime(millisUntilFinished);
            }

            @Override
            public void onFinish() {
                ClockService.TASK_FINISHED_FLAG = 1;
            }
        };
        countDownTimer.start();
    }

    @Override
    public void onCountDownFinished() {

    }


    /*    public void startTiming(long duration){
            CountDownTimer countDownTimer = new CountDownTimer(duration,1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    formatTime(millisUntilFinished);
                }

                @Override
                public void onFinish() {

                }
            };
            countDownTimer.start();
        }*/
    private void formatTime(long millisUntilFinished) {
        Intent intent = new Intent(ClockService.ACTION_TIME_TICK);
        // 把剩余的时间换算成秒
        long seconds = millisUntilFinished / 1000;
        // 如果seconds > 60
        if (seconds >= 60) {
            long minute = seconds / 60;
            seconds = seconds % 60;
            // 如果时长小于60分钟
            if (minute < 60) {
                //  view.setText(minute+":"+seconds);
                intent.putExtra(ClockService.TEXT_TIME, minute + ":" + seconds);
                sendBroadcast(intent);
               // System.out.println(minute+"-"+seconds+"-"+intent);
            }
        } else {
            //view.setText("00:"+seconds);
            intent.putExtra(ClockService.TEXT_TIME, "00:" + seconds);
            sendBroadcast(intent);
        }
    }
    private void initReceiver(){
        timeCountReceiver = new TimeCountReceiver();
        intentFilter = new IntentFilter();
        intentFilter.addAction(ClockService.ACTION_TASK_TOBACK);
        registerReceiver(timeCountReceiver,intentFilter);
    }
}
